package xgenerator.ui.swing;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import xgenerator.ui.swing.model.ConnectionStringComboBoxModel;
import xgenerator.ui.swing.model.DbTypeComboBoxModel;
import xgenerator.ui.swing.model.DriverClassNameComboBoxModel;

public class CreateDataSourcesDialog extends JDialog implements UIConstants {

	private JTextField dataSourceNameField;
	private JComboBox dbTypeComboBox;
	private JComboBox driverClassNameComboBox;
	private JComboBox urlComboBox;
	private JTextField userNameField;
	private JTextField passwordField;
	private JButton saveNewBtn;
	private JButton resetBtn;
	
	public CreateDataSourcesDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		initComponents();
	}

	private void initComponents() {
		this.setTitle("添加数据源");
		this.setIconImage(IconUtils.createIconImage(APP_ICON_IMAGE));
		this.setContentPane(buildContentPane());

		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		//this.setSize(screenWidth / 2, screenHeight / 2);
		this.setSize(400, 200);
		this.setLocation((screenWidth - getWidth()) / 2, (screenHeight - getHeight()) / 2);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(false);
	}
	
	private JPanel buildContentPane() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(7, 2));

		panel.add(new JLabel("数据源名："));
		this.dataSourceNameField = new JTextField();
		panel.add(this.dataSourceNameField);
		
		panel.add(new JLabel("数据库类型："));
		this.dbTypeComboBox = new JComboBox(new DbTypeComboBoxModel());
		panel.add(this.dbTypeComboBox);
		
		panel.add(new JLabel("驱动类名："));
		this.driverClassNameComboBox = new JComboBox(new DriverClassNameComboBoxModel());
		this.driverClassNameComboBox.setEditable(true);
		panel.add(this.driverClassNameComboBox);
		
		panel.add(new JLabel("URL："));
		this.urlComboBox = new JComboBox(new ConnectionStringComboBoxModel());
		panel.add(this.urlComboBox);
		
		panel.add(new JLabel("用户名："));
		this.userNameField = new JTextField();
		panel.add(this.userNameField);
		
		panel.add(new JLabel("密码："));
		this.passwordField = new JTextField();
		panel.add(this.passwordField);

		this.saveNewBtn = new JButton("保存");
		panel.add(this.saveNewBtn);
		
		this.resetBtn = new JButton("重置");
		panel.add(this.resetBtn);
		
		return panel;
	}
	

}
